const db = require('./db.js')

exports.getGoods = async (query) => {
    try {
        let { _page = 1 } = query
        if (_page < 1) {
            _page = 1
        }
        const _size = 10
        const _start = (_page - 1) * _size
        const sqlStr = `select id,CONCAT(left(title,10), '...') title,add_time,zhaiyao,click,img_url,sell_price,market_price,stock_quantity from cms_goods order by id desc limit ${_start},${_size}`
        return await db.query(sqlStr)
    } catch (err) {
        throw err
    }
}

exports.getGoodsById = async (id) => {
    try {
        const sqlStr = `select id,title,add_time,goods_no,stock_quantity,market_price,sell_price,content from cms_goods where id=${id}`
        const [goods] = await db.query(sqlStr)
        return goods
    } catch (err) {
        throw err
    }
}

exports.getGoodsByIds = async (ids) => {
    try {
        // 参考 https://www.cnblogs.com/liaozt/p/6876420.html
        const sqlStr = `SELECT id,title,sell_price,img_url thumb_path from cms_goods where id in (${ids})  ORDER BY FIELD(id, ${ids})`        
        return await db.query(sqlStr)
    } catch (err) {
        throw err
    }
}